package com.zhangli.mysockettest;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button start_button;
    Button conn_button;
    EditText edit;
    TextView text;
    private SocketClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_layout);

        start_button = (Button) findViewById(R.id.start_button);
        conn_button = (Button) findViewById(R.id.conn_button);
        edit= (EditText) findViewById(R.id.edit);
        text= (TextView) findViewById(R.id.text);

        conn_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                client=new SocketClient ();

                //服务端的IP地址和端口号
                client.clintValue (getApplicationContext(),"192.168.0.48" ,9999);
                //开启客户端接收消息线程
                client.openClientThread ();
            }
        });
        start_button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                client.sendMsg (edit.getText().toString());
            }
        });

        SocketClient.mHandler=new Handler(  ){
            @Override
            public void handleMessage(Message msg)
            {
                text.setText(msg.obj.toString ());
            }
        };
    }

}
